package com.qyb.demo.threadpool1;

import java.util.concurrent.*;

/**
 * <strong>实现功能:</strong>
 * <p></p>
 *
 * @author 秦有宝
 * @version 1.0.0  2019/1/23 9:41
 */
public class ThreadFactoryDemo {

    public static void demo(){
        //定义ExecutorService实例
        ExecutorService executorService = new ThreadPoolExecutor(5, 5, 0L, TimeUnit.MILLISECONDS,
                new SynchronousQueue<Runnable>(), new ThreadFactory() {
            @Override
            public Thread newThread(Runnable r) {
                Thread t = new Thread(r);
                System.out.println("我是线程" + r);
                return t;
            }
        });

        //lambda表达式编写方法体中的逻辑
        Runnable run = () ->{
          try {
              Thread.sleep(1000);
              System.out.println(Thread.currentThread().getName() + "正在运行");
          }catch (InterruptedException e){
              e.printStackTrace();
          }
        };

        for (int i = 0; i < 5; i++) {
            executorService.submit(run);
        }
        //一定要做关闭
        executorService.shutdown();
    }

}
